#Region " Imports "

Imports BA.XML
Imports BA.Attachments
Imports BA.Images
Imports System.IO

#End Region

Partial Class Usercontrols_Controlpanel_Profile
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "



#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("My Profile")

        If IsUserAuthenticated = True Then

            ConfigBaseEditorCtrls()

            If Me.Page.IsPostBack = False Then
				BuildFCKEditor(EditorPageType.ForumEditor)
                LoadPage()
                LoadProfile()
                LoadThemeImage(Me.uxThemeImage, Me.uxThemeDropDownList.SelectedValue.ToString)
                LoadAvatar(Me.uxAvatarImage, CurrentUser, True)
            End If
        End If

        LoadSettings()

        Me.uxMemberPanel.Visible = ShowControls(SecurityMask.Member, True)

    End Sub

#End Region

#Region " Save Data "

    Protected Sub SaveProfile()
        Dim MU As MembershipUser

        MU = Membership.GetUser(CurrentUser)
        MU.Email = Me.uxEmailTextBox.Text.ToString.Trim

        Membership.UpdateUser(MU)

        Profile.User.Name = Me.uxFullNameTextBox.Text.Trim
        Profile.User.Age = Me.uxAgeTextBox.Text.Trim
        Profile.User.Occupation = Me.uxOccupationTextBox.Text.Trim
        Profile.User.Location = Me.uxLocationTextBox.Text.Trim
        Profile.User.Website = Me.uxWebsiteTextBox.Text.Trim
        Profile.User.DisplayHideProfile = Me.uxHideProfileCheckBox.Checked
        Profile.User.DisplayHideEmail = Me.uxHideEmailCheckBox.Checked

        Profile.IM.AOL = Me.uxAOLTextBox.Text.Trim
        Profile.IM.ICQ = Me.uxICQTextBox.Text.Trim
        Profile.IM.MSN = Me.uxMSNTextBox.Text.Trim
        Profile.IM.YAHOO = Me.uxYahooTextBox.Text.Trim

        Profile.UI.Theme = Me.uxThemeDropDownList.SelectedValue.Trim

		Profile.UI.Signature = Me.uxSignatureBodyTextbox.Value

		Profile.Site.PageSize = Me.uxPageSizeDropDownList.SelectedValue
		Profile.Site.HighLight = Me.uxHighLightResultsCheckBox.Checked
		Profile.Site.HighLightExactMatch = Me.uxHighLightExactMatchesCheckBox.Checked
		Profile.Site.ReceiveEmailNotification = Me.uxReceiveEmailNotificationCheckBox.Checked
		Profile.Site.ReceiveNewsLetter = Me.uxReceiveNewsLetterCheckBox.Checked
		Profile.Site.EnableSmilies = Me.uxEnableSmiliesCheckBox.Checked

	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadPage()
		LoadPageSize(Me.uxPageSizeDropDownList)
		LoadThemeList(Me.uxThemeDropDownList)
	End Sub

	Private Sub LoadSettings()
		uxFavoritesTabPanel.Enabled = EnableFavorites
		uxTagsTabPanel.Enabled = EnableTags
		uxGalleryTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
		uxGallery.IsLoadedAsOption = True
		uxPendingPosts.IsLoadedAsOption = True

		If EnableEmailNotifications Then
			Me.uxReceiveEmailNotificationCheckBox.Text = Resources.Resource.RecieveEmailSentence
			Me.uxReceiveNewsLetterCheckBox.Text = Resources.Resource.ReceiveNewsletterSentence
		Else
			Me.uxReceiveEmailNotificationCheckBox.Text = Resources.Resource.AdminDisabledEmailNotifications
			Me.uxReceiveNewsLetterCheckBox.Text = Resources.Resource.AdminDisabledEmailNotifications
			Me.uxReceiveEmailNotificationCheckBox.Enabled = False
			Me.uxReceiveNewsLetterCheckBox.Enabled = False
		End If

		If EnableSmilies Then
			Me.uxEnableSmiliesCheckBox.Text = Resources.Resource.EnableSmiliesSentence
		Else
			Me.uxEnableSmiliesCheckBox.Text = Resources.Resource.AdminDisabledSmilies
			Me.uxEnableSmiliesCheckBox.Enabled = False
		End If

	End Sub

	Protected Sub LoadProfile()
		Me.uxFullNameTextBox.Text = Profile.User.Name
		Me.uxEmailTextBox.Text = Membership.GetUser(CurrentUser).Email
		Me.uxAgeTextBox.Text = Profile.User.Age
		Me.uxOccupationTextBox.Text = Profile.User.Occupation
		Me.uxLocationTextBox.Text = Profile.User.Location
		Me.uxWebsiteTextBox.Text = Profile.User.Website
		Me.uxHideProfileCheckBox.Checked = Profile.User.DisplayHideProfile
		Me.uxHideEmailCheckBox.Checked = Profile.User.DisplayHideEmail

		Me.uxAOLTextBox.Text = Profile.IM.AOL
		Me.uxICQTextBox.Text = Profile.IM.ICQ
		Me.uxMSNTextBox.Text = Profile.IM.MSN
		Me.uxYahooTextBox.Text = Profile.IM.YAHOO

		Me.uxThemeDropDownList.SelectedValue = Profile.UI.Theme.Trim

		Me.uxSignatureBodyTextbox.Value = Profile.UI.Signature

		Me.uxPageSizeDropDownList.SelectedValue = Profile.Site.PageSize
		Me.uxHighLightResultsCheckBox.Checked = Profile.Site.HighLight
		Me.uxHighLightExactMatchesCheckBox.Checked = Profile.Site.HighLightExactMatch
		Me.uxReceiveEmailNotificationCheckBox.Checked = Profile.Site.ReceiveEmailNotification
		Me.uxReceiveNewsLetterCheckBox.Checked = Profile.Site.ReceiveNewsLetter
		Me.uxEnableSmiliesCheckBox.Checked = Profile.Site.EnableSmilies

		GetRolesForUser()

	End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        SaveProfile()
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
        ResetProfile()
    End Sub

    Protected Sub uxThemeDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxThemeDropDownList.SelectedIndexChanged
        LoadThemeImage(Me.uxThemeImage, Me.uxThemeDropDownList.SelectedValue.ToString)
    End Sub

    Protected Sub uxTryItButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxTryItButton.Click
        Profile.UI.Theme = Me.uxThemeDropDownList.SelectedValue.Trim
        Me.uxProfileTabContainer.ActiveTab = Me.uxInterfaceInformationTabPanel

		Response.Redirect(String.Format("{0}.aspx?pid=cp_profile", AppPage))

		'ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "repost", "document.forms[0].submit();", True)

    End Sub

    Protected Sub uxSaveAvatarButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveAvatarButton.Click
        Dim Attachment As New AttachmentUtility
        Dim ImageFile As New ImageUtility
        Dim Thumb As Byte()

        ImageFile.FileField = Me.uxAvatarFileUpload

        Attachment.Image = ImageFile.GetImageFromPostedFile(Me.uxAvatarFileUpload)

        If Attachment.Image.Width > 65 OrElse Attachment.Image.Height > 65 Then
            Thumb = ImageFile.CreateThumbnail(Attachment.GetStreamFromImage, 65, 65)
            Profile.User.Picture = Thumb
        Else
            Profile.User.Picture = Attachment.GetByteArrayFromImage
        End If

        Profile.User.PictureType = Me.uxAvatarFileUpload.PostedFile.ContentType

        LoadAvatar(Me.uxAvatarImage, CurrentUser, True)
    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetRolesForUser()
        Dim Role As New Object

        For Each Role In Roles.GetRolesForUser
            Me.uxRolesListBox.Items.Add(Role.ToString)
        Next

    End Sub

    Protected Sub ResetProfile()

    End Sub

    Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxSignatureBodyTextbox
    End Sub

#End Region

End Class
